<?php
/**
 * Created by PhpStorm.
 * User: von
 * Date: 2017/11/28
 * Time: 18:01
 */
namespace backend\controllers;

use backend\models\MyPackage;
use common\helps\pageTools;
use common\helps\profiles;
use common\models\Http;
use common\models\PackageCity;
use common\models\Responses;
use yii\data\Pagination;
use yii\db\Query;

class MyPackageController extends BaseController {
    const LIMIT = 10; //每页显示个数
    const GET_MY_PACKAGE_DATA = 'my-package/get-my-package-data';
    /*
     * 我参与的资产包
     * 查询条件
     *@params :$user_id  我的id
     *         $packageState 资产包状态//1无、2启动尽调、3已立项、4已评估、5已受让、6待跟进、
     *         $page   页数
     *         $keywords     关键词
     *         $packageGroup 分类  //1 我参与的资产包  2 我上传的资产包  3 我关注的资产包
     */
    public function actionGetMyPackage(){
            $data['userId']         = \Yii::$app->user->id;
            $data['packageState']  = \Yii::$app->request->get('packageState',123);
            $data['page']           = trim(\Yii::$app->request->get('page',1));
            $data['keywords']       = trim(\Yii::$app->request->get('keywords',''));
            $data['packageGroup']  = trim(\Yii::$app->request->get('packageGroup',1));
            $data['orderGroup'] =   \Yii::$app->request->get('orderGroup',0);     //字段排序
        if(empty( $data['page']  )) {
                $data['page']   = 1;
            }
            $data['limit']    = intval(\Yii::$app->request->get('showNum',self::LIMIT));
            if(strpos( $data['page']  ,'n') !== false){
                $data['page']   = intval(substr($data['page'],1));
            }
            //分页个数
            $params['page']     = !empty($data['page']) ? $data['page'] : \Yii::$app->params['PAGE_HOME'];
            $params['userId']   = $data['userId'];
            $params['status']   = $data['packageState'];
            $params['packageGroup']   = $data['packageGroup'];
            $params['orderGroup']   =  $data['orderGroup'];
            $params['keywords']   =  $data['keywords'];
            $params['packageState']   =  $data['packageState'];
            $params['showNum']      = $data['limit'];
            if(!empty($data['keywords'])){
                $params['keywords']= $data['keywords'];
            }
            $data['userPackageCount'] = self::actionGetMyPackageCount($params,$data['limit']);
            //分页
            switch ( $data['packageState']){
                case \Yii::$app->params['ALL']:
                    $pages = new Pagination(['totalCount' => $data['userPackageCount']['allState'],'pageSize' => $data['limit']]);
                    break;
                case \Yii::$app->params['NONE']:
                    $pages = new Pagination(['totalCount' => $data['userPackageCount']['wuState'],'pageSize' => $data['limit']]);
                    break;
                case \Yii::$app->params['YUPINGGU']:
                    $pages = new Pagination(['totalCount' => $data['userPackageCount']['qidongState'],'pageSize' => $data['limit']]);
                    break;
                case \Yii::$app->params['LIXIANGSHENPIN']:
                    $pages = new Pagination(['totalCount' => $data['userPackageCount']['lixiangState'],'pageSize' => $data['limit']]);
                    break;
                default:
                    $pages = new Pagination(['totalCount' => $data['userPackageCount']['allState'],'pageSize' => $data['limit']]);
                    break;
            }
//            $pages = new Pagination(['totalCount' => 100,'pageSize' => $data['limit']]);

            $data['userPackageData'] = json_decode(self::actionGetMyPackageData($params,$data['limit'],$pages),true);
            //获取资产包状态
            $data['packageStateName'] = profiles::getPakcageState();
            $data['pages']  = $pages;
            return $this->render('index',$data);

    }

    /*
     * 获取我的资产包个数
     */
    public static function actionGetMyPackageCount($params,$limit){
        $conditions['userId']       = $params['userId'];
        $conditions['packageState'] = $params['status'];
        $conditions['limit']        = $limit;
        $conditions['page']         = $params['page'];
        $conditions['keywords']     = $params['keywords'];//搜索关键字
        $conditions['orderGroup']     = $params['orderGroup'];//搜索关键字
        $conditions['packageGroup']   = $params['packageGroup'];
//        $PackageDataCount = MyPackage::getUploadMyPackageData($conditions,1);
//        $allState             = MyPackage::getMyPackageData($conditions,1,\Yii::$app->params['ALL']);

//die($allState);
        if (!empty($conditions)){
            switch ($conditions['packageGroup']){
                case \Yii::$app->params['UPDATE_PACKAGE']:
                    //获取总个数
                    //$conditions 为筛选条件
                    //1无、2预评估、3立项审批中、4投前尽调、5待跟进、123全部
                $wuState          = MyPackage::getUploadMyPackageData($conditions,1,\Yii::$app->params['NONE']);
                    $qidongState          = MyPackage::getUploadMyPackageData($conditions,1,\Yii::$app->params['YUPINGGU']);
                    $lixiangState          = MyPackage::getUploadMyPackageData($conditions,1,\Yii::$app->params['LIXIANGSHENPIN']);
                    $pingguState          = MyPackage::getUploadMyPackageData($conditions,1,\Yii::$app->params['TOUQIANJINDIAO']);
                    $shourangState          = MyPackage::getUploadMyPackageData($conditions,1,\Yii::$app->params['DAIGENJIN']);
                    $allState             = MyPackage::getUploadMyPackageData($conditions,1,\Yii::$app->params['ALL']);
                    $PackageDataCount     = MyPackage::getUploadMyPackageData($conditions,1);
                    $count = [
                        'wuState'=>$wuState,
                        'qidongState'=>$qidongState,
                        'lixiangState'=>$lixiangState,
                        'pingguState'=>$pingguState,
                        'shourangState'=>$shourangState,
                        'allState'=>$allState,
                        'PackageDataCount'=>$PackageDataCount,
                    ];
                    break;
                case \Yii::$app->params['FOLLOW_PACKAGE']:
                        //获取总个数
                        //$conditions 为筛选条件
                        //1 为是查询总条数
                        $wuState            = MyPackage::getMyFollowPackageData($conditions,1,\Yii::$app->params['NONE']);
                        $qidongState        = MyPackage::getMyFollowPackageData($conditions,1,\Yii::$app->params['YUPINGGU']);
                        $lixiangState       = MyPackage::getMyFollowPackageData($conditions,1,\Yii::$app->params['LIXIANGSHENPIN']);
                        $pingguState        = MyPackage::getMyFollowPackageData($conditions,1,\Yii::$app->params['TOUQIANJINDIAO']);
                        $shourangState      = MyPackage::getMyFollowPackageData($conditions,1,\Yii::$app->params['DAIGENJIN']);
                    $allState             = MyPackage::getMyFollowPackageData($conditions,1,\Yii::$app->params['ALL']);
                    $PackageDataCount   = MyPackage::getMyFollowPackageData($conditions,1);
                        $count = [
                            'wuState'=>$wuState,
                            'qidongState'=>$qidongState,
                            'lixiangState'=>$lixiangState,
                            'pingguState'=>$pingguState,
                            'shourangState'=>$shourangState,
                            'allState'=>$allState,
                            'PackageDataCount'=>$PackageDataCount,
                        ];
                    break;
                default:
                        //获取总个数
                        //$conditions 为筛选条件
                        //1 为是查询总条数
                        $wuState              = MyPackage::getMyPackageData($conditions,1,\Yii::$app->params['NONE']);
                        $qidongState          = MyPackage::getMyPackageData($conditions,1,\Yii::$app->params['YUPINGGU']);
                        $lixiangState         = MyPackage::getMyPackageData($conditions,1,\Yii::$app->params['LIXIANGSHENPIN']);
                        $pingguState          = MyPackage::getMyPackageData($conditions,1,\Yii::$app->params['TOUQIANJINDIAO']);
                        $shourangState        = MyPackage::getMyPackageData($conditions,1,\Yii::$app->params['DAIGENJIN']);
                        $allState             = MyPackage::getMyPackageData($conditions,1,\Yii::$app->params['ALL']);
                        $PackageDataCount     = MyPackage::getMyPackageData($conditions,1);
                        $count = [
                            'wuState'=>$wuState,
                            'qidongState'=>$qidongState,
                            'lixiangState'=>$lixiangState,
                            'pingguState'=>$pingguState,
                            'shourangState'=>$shourangState,
                            'allState'=>$allState,
                            'PackageDataCount'=>$PackageDataCount,
                        ];
                    break;
            }
            return $count;
        }
        return json_encode(Responses::response400("查询出错"));
    }
    /*
     * 获取我的资产包信息
     */
    public static function actionGetMyPackageData($params,$limit,$pages=0){
        $conditions['userId']       = $params['userId'];
        $conditions['packageState'] = $params['status'];
        $conditions['limit']        = $limit;
        $conditions['page']         = $params['page'];
        $conditions['keywords']     = $params['keywords'];//搜索关键字
        $conditions['packageGroup']   = $params['packageGroup'];
        $conditions['orderGroup']   = $params['orderGroup'];
//        $PackageDataCount = MyPackage::getUploadMyPackageData($conditions,1);
//die($PackageDataCount);die;
        if (!empty($conditions)){
            switch ($conditions['packageGroup']){
                case \Yii::$app->params['UPDATE_PACKAGE']:
                    $PackageData = [];
                    if (!empty($pages)) {
                        $PackageData = MyPackage::getUploadMyPackageData($conditions, 0, 0, $pages);
                    }
                    break;
                case \Yii::$app->params['FOLLOW_PACKAGE']:
                    $PackageData = [];
                    if (!empty($pages)) {
                        $PackageData = MyPackage::getMyFollowPackageData($conditions, 0, 0, $pages);
                    }
                    break;
                default:
                    $PackageData = [];
                    if (!empty($pages)) {
                        $PackageData = MyPackage::getMyPackageData($conditions, 0, 0, $pages);
                    }
                    break;
            }

            //资产包城市多选 package_city 中查找
            $countryList = \common\models\Country::getTitleList();
            if (!empty($PackageData) && is_array($PackageData)) {
                foreach ($PackageData as $k=>$v) {
                    //资产包城市多选
                    $cityNameList = '';
                    if ($v['city']!= '') {
                        $arr = explode(',',$v['city']);
                        $array = array();
                        if (!empty($arr)) {
                            foreach ($arr as $value) {
                                $array[] = $countryList[(int)$value];
                            }
                        }
                        $cityNameList = join('、', $array);
                    }
                    $PackageData[$k]['cityNameList'] = $cityNameList;
                }
            }

            return json_encode(Responses::response200($PackageData));
        }
        return json_encode(Responses::response400("查询出错"));
    }
}
